
const { randomUUID } = require("crypto");
const { addSubject, querySubject, deltetSubject, getSubjectDetail } = require("../../../controller/context/subject/subject-conroller");
const { serverError } = require("../../../utils");
const { emptyCheck } = require("../../../utils/checkEmpty");

/**
 * @description 内容路由
 * @author 韩
 */
const router = require("koa-router")()
router.prefix("/subject");

// 添加或编辑
router.post("/save", async (ctx, next) => {
    try {

        const {        // shopId,
            id = null, title, recommend, imgUrl
        } = ctx.request.body
        const check = emptyCheck({ title })
        if (check.code !== 0) {
            ctx.body = check
        } else {
            const res = await addSubject({
                // _id: _id,
                id,
                title,
                recommend,
                imgUrl,

            })
            ctx.body = res
        }
    } catch (error) {
        console.log(error)
        ctx.body = serverError
    }
})

// // 查询列表
router.post("/list", async (ctx, next) => {
    try {
        const { current, pageSize, title } = ctx.request.body;
        const res = await querySubject({
            current, pageSize, title
        })
        ctx.body = res
    } catch (error) {
        ctx.body = serverError
    }

})


// // 删除
router.delete("/delete", async (ctx, next) => {
    try {
        const id = ctx.query.id || undefined;
        if (!id) {
            ctx.body = {
                code: '0011',
                msg: "未选择需要删除的数据"
            }
        }
        else {
            const res = await deltetSubject({ id })
            ctx.body = res;
        }
    } catch (error) {
        ctx.body = serverError
    }
})

// // 查看专题详情
router.get("/detail", async (ctx, next) => {
    try {
        const id = ctx.query.id || undefined;
        const check = emptyCheck({ id })
        if (check.code !== 0) {
            ctx.body = check;
        } else {
            const res = await getSubjectDetail({ id: id })
            ctx.body = res;
        }

    } catch (error) {
        ctx.body = serverError
    }
})

module.exports = router